package com.heima.freemarker.controller;

import com.heima.freemarker.pojo.Student;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;


@Controller
@RequestMapping("/hello")
public class HelloController {

    @GetMapping("/basic")
    public String hello(Model model) {
        //1.封装数据
        model.addAttribute("name","张三");
        Student student = new Student();
        student.setName("二狗子");
        student.setAge(12);
        model.addAttribute("stu",student);
        //2.跳转页面

        return "01-basic";
    }

    @GetMapping("/modelMap")
    public String hello2(Model model) {
        //1.封装数据
        model.addAttribute("name","张三222");
        Student student = new Student();
        student.setName("二狗子");
        student.setAge(12);
        model.addAttribute("stu",student);
        //2.跳转页面

        return "01-basic";
    }

    @GetMapping("/mav")
    public ModelAndView hello3() {
        ModelAndView mav = new ModelAndView();
        //1.封装数据
        mav.addObject("name","张三");
        Student student = new Student();
        student.setName("二狗子");
        student.setAge(12);
        mav.addObject("stu",student);
        //2.跳转页面

        mav.setViewName("01-basic");
        return mav;
    }

    @GetMapping("/list")
    public String list(Model model){

        //------------------------------------
        Student stu1 = new Student();
        stu1.setName("小强");
        stu1.setAge(18);
        stu1.setMoney(1000.86d);

        //小红对象模型数据
        Student stu2 = new Student();
        stu2.setName("小红");
        stu2.setMoney(200.1d);
        stu2.setAge(19);

        //将两个对象模型数据存放到List集合中
        List<Student> stus = new ArrayList<>();
        stus.add(stu1);
        stus.add(stu2);

        //向model中存放List集合数据
        model.addAttribute("stus",stus);

        //------------------------------------

        Map resultMap = new HashMap<>();
        resultMap.put("stu1",stu1);
        resultMap.put("stu2",stu2);
        model.addAttribute("resultMap",resultMap);

        //创建Map数据
        HashMap<String,Student> stuMap = new HashMap<>();
        stuMap.put("stu1",stu1);
        stuMap.put("stu2",stu2);
        // 3.1 向model中存放Map数据
        model.addAttribute("stuMap", stuMap);

        return "02-list";
    }
}
